//
//  CROneSentenceViewController.m
//  NewAge
//
//  Created by 王鑫 on 15/11/30.
//  Copyright © 2015年 ColorRevolution. All rights reserved.
//

#import "CROneSentenceViewController.h"
#import "CRSentenceModel.h"
#import "CRArticleModel.h"
#import "CRQuestionModel.h"
#import "CROneSentenceCollectionViewCell.h"
#import "CRArticleCollectionViewCell.h"
#import "CRQuestionCollectionViewCell.h"

@interface CROneSentenceViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *array;
@property (nonatomic, strong) UISegmentedControl *segmentControl;
@property (nonatomic, strong) MBProgressHUD *hud;
@property (nonatomic, strong) CRSentenceModel *sentence;
@property (nonatomic, strong) CRArticleModel *article;
@property (nonatomic, strong) CRQuestionModel *question;

@end

@implementation CROneSentenceViewController

#pragma mark - Method viewWillAppear

- (void)viewWillAppear:(BOOL)animated {
    // segment的初始化选择
    self.segmentControl.selectedSegmentIndex = self.collectionView.contentOffset.x / WIDTH;
    self.segmentControl.selectedSegmentIndex = 0;
    self.segmentControl.hidden = NO;
}

#pragma mark - Method viewDidLoad

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.133f green:0.125f blue:0.125f alpha:1.00f];
    [self createSegment];
    [self createCollectionView];
    [self createData];
    
}



#pragma mark - Method createSegment

- (void)createSegment {
    if (!_segmentControl) {
        self.segmentControl = [[UISegmentedControl alloc] initWithItems:@[@"段落", @"文章", @"问题"]];
        self.segmentControl.frame = CGRectMake(WIDTH / 2 - 70, 25, 140, 28);
        self.segmentControl.layer.borderColor = [[UIColor whiteColor] CGColor];
        self.segmentControl.tintColor = [UIColor whiteColor];
        [self.navigationController.view addSubview:self.segmentControl];
        [self.segmentControl addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    }
}

- (void)segAction:(UISegmentedControl *)seg {
    if (seg.selectedSegmentIndex == 0) {
        self.collectionView.contentOffset = CGPointMake(0, 0);
    } else if (seg.selectedSegmentIndex == 1) {
        self.collectionView.contentOffset = CGPointMake(WIDTH, 0);
    } else {
        self.collectionView.contentOffset = CGPointMake(WIDTH * 2, 0);
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.collectionView) {
        if (self.collectionView.contentOffset.x == 0) {
            self.segmentControl.selectedSegmentIndex = 0;
        } else if (self.collectionView.contentOffset.x == WIDTH) {
            self.segmentControl.selectedSegmentIndex = 1;
        } else {
            self.segmentControl.selectedSegmentIndex = 2;
        }
    }
}



#pragma mark - Method createCollectionView

- (void)createCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    flowLayout.itemSize = CGSizeMake(screenSize.width, screenSize.height - 49);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:self.collectionView];
    self.collectionView.pagingEnabled = YES;
    self.collectionView.bounces = NO;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.collectionView registerClass:[CROneSentenceCollectionViewCell class] forCellWithReuseIdentifier:@"OneSentenceCell"];
    [self.collectionView registerClass:[CRArticleCollectionViewCell class] forCellWithReuseIdentifier:@"OneArticleCell"];
    [self.collectionView registerClass:[CRQuestionCollectionViewCell class] forCellWithReuseIdentifier:@"OneQuestionCell"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 3;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        CROneSentenceCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"OneSentenceCell" forIndexPath:indexPath];
        cell.contentView.backgroundColor = [UIColor whiteColor];
    cell.model = self.sentence;
        
        return cell;
    } else if (indexPath.row == 1) {
        CRArticleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"OneArticleCell" forIndexPath:indexPath];
        cell.contentView.backgroundColor = [UIColor redColor];
        NSLog(@"%@", self.article);
        cell.model = self.article;
        
        return cell;
    } else {
        CRQuestionCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"OneQuestionCell" forIndexPath:indexPath];
        cell.contentView.backgroundColor = [UIColor yellowColor];
        cell.model = self.question;
        return cell;
    }

}



#pragma mark - Method createData

- (void)createData {
    AFNetworkReachabilityManager *reManager = [AFNetworkReachabilityManager sharedManager];
    [reManager startMonitoring];
    [reManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        // 判断网络状态
        if (status == 1 || status == 2) {
            // sentence页面网络请求
            NSDate *sentenceDate = [NSDate date];
            NSDateFormatter *sentenceFormatter =[[NSDateFormatter alloc] init];
            sentenceFormatter.dateFormat = @"yyyy-MM-dd";
            NSString *sentenceStrDate = [sentenceFormatter stringFromDate:sentenceDate];
            NSDictionary *sentencePostDic = @{@"strDate":sentenceStrDate, @"strRow":@"1"};

            [AfNetWorkingManager postWithParameters:sentencePostDic Url:@"http://rest.wufazhuce.com/OneForWeb/one/getHp_N" block:^(NSDictionary *resultDic, NSError *error) {
                NSDictionary *dic = resultDic[@"hpEntity"];
                self.sentence = [[CRSentenceModel alloc] init];
                [self.sentence setValuesForKeysWithDictionary:dic];
                NSLog(@"self.model%@", self.sentence);
                [self.collectionView reloadData];
            }];
            // article页面网络请求
            NSDate *articleDate = [NSDate date];
            NSDateFormatter *articleFormatter = [[NSDateFormatter alloc] init];
            articleFormatter.dateFormat = @"yyyy-MM-dd";
            NSString *articleStrDate = [articleFormatter stringFromDate:articleDate];
            NSDictionary *articlePostDic = @{@"strDate":articleStrDate, @"strRow":@"1"};
            
            [AfNetWorkingManager postWithParameters:articlePostDic Url:@"http://rest.wufazhuce.com/OneForWeb/one/getC_N" block:^(NSDictionary *resultDic, NSError *error) {
                NSDictionary *dic = resultDic[@"contentEntity"];
                self.article = [[CRArticleModel alloc] init];
                [self.article setValuesForKeysWithDictionary:dic];
                
                [self.collectionView reloadData];
                [self.hud removeFromSuperview];
            }];
            // question页面请求
            NSDate *questionDate = [NSDate date];
            NSDateFormatter *questionFormatter = [[NSDateFormatter alloc] init];
            questionFormatter.dateFormat = @"yyyy-MM-dd";
            NSString *questionStrDate = [questionFormatter stringFromDate:questionDate];
            NSDictionary *questionPostDic = @{@"strDate":questionStrDate, @"strRow":@"1"};
            [AfNetWorkingManager postWithParameters:questionPostDic Url:@"http://rest.wufazhuce.com/OneForWeb/one/getQ_N" block:^(NSDictionary *resultDic, NSError *error) {
                NSDictionary *dic = resultDic[@"questionAdEntity"];
                NSLog(@"%@", dic);
                self.question = [[CRQuestionModel alloc] init];
                [self.question setValuesForKeysWithDictionary:dic];
                
                [self.collectionView reloadData];
            }];
            
        } else {
            [self.hud removeFromSuperview];
            MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
            [self.view addSubview:hud];
            hud.mode = MBProgressHUDModeText;
            hud.labelText = @"数据请求失败,请检查网络后重试";
            [hud showAnimated:YES whileExecutingBlock:^{
                sleep(1);
            } completionBlock:^{
                [hud removeFromSuperview];
                
            }];
        }
        
    }];
    // 停止检测
    [reManager stopMonitoring];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
